<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Wy;

use think\Db;

//内容
class Content {
	private $all_data_url = array();
    public function __construct(){
    }
	// 这个表的数据量
	public function get_table_num($_db_name){
		$_cache_name = 'sysadmin_content_table_count_'.$_db_name;
		$num = cache($_cache_name);
		if($num){
			return $num;
		}
		$num = Db::table($_db_name)->count();
		cache($_cache_name,$num,600);
		return $num;
	}
	// 指定栏目的数据量
	public function get_cat_num($_db_name,$cat_id=0){
		$_cache_name = 'sysadmin_content_table_count_'.$_db_name.'_cat_id_'.$cat_id;
		$num = cache($_cache_name);
		if($num){
			return $num;
		}
		if($cat_id > 0){
			$num = Db::table($_db_name)->where('cat_id','=',$cat_id)->count();
		}else{
			$num = Db::table($_db_name)->count();
		}
		cache($_cache_name,$num,600);
		return $num;
	}

	public function get_cat_url($cat_id){
		$_all_data_url = cache('cat_all_data_url');
		if(!$_all_data_url){
			$_cat_lists = Db::name('content_cat')->field('id,type,up_id,route')->where('is_del','=','0')->where('status','=','1')->order('sort asc,id asc')->select();

			$_data_cat = self::cat_package($_cat_lists,0);

			$_all_data_url = $this->all_data_url;
			cache('cat_all_data_url',$_all_data_url);
		}
		$_url = isset($_all_data_url[$cat_id])?$_all_data_url[$cat_id]:'javascript:void(0);';
		return $_url;
	}

	public function cat_package($_cat_lists=array(),$_up_id=0){
		$_data = array();
		if($_cat_lists){
			foreach($_cat_lists as $k=>$v){
				if($v['up_id'] == $_up_id){
					$_son_lists = self::cat_package($_cat_lists,$v['id']);

					$v_url = 'javascript:void(0);';
					switch ($v['type']){
					case 1:
						if($v['route']){
							$v_url = url('/'.$v['route'],'',true,PC_DOMAIN);
						}else{
							$v_url = url('/lists/'.$v['id'],'',true,PC_DOMAIN);
						}

						break;
					case 2:
						if($v['route']){
							$v_url = url('/'.$v['route'],'',true,PC_DOMAIN);
						}else{
							$v_url = url('/content/'.$v['id'],'',true,PC_DOMAIN);
						}

						break;
					case 3:
						if($v['route']){
							$v_url = url('/'.$v['route'],'',true,PC_DOMAIN);
						}else{
							$v_url = url('/category/'.$v['id'],'',true,PC_DOMAIN);
						}

						break;
					case 4:
						$v_url = isset($_son_lists[0])?$_son_lists[0]['url']:'javascript:void(0);';

						break;
					case 0:
						$v_url = $v['route']?$v['route']:'javascript:void(0);';

						break;
					default:
						$v_url = 'javascript:void(0);';

					}
					$v['son_lists'] = $_son_lists;

					$v['url'] = $v_url;

					$this->all_data_url[$v['id']] = $v_url;
					$_data[] = $v;
				}
			}
		}
		return $_data;
	}

	public function getSonLists($upid=0,$space=''){
		$icon = array(
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│',
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├─',
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└─'
		);  //格式化的字符

		$_where = array();
		$_where[] = array('is_del','=','0');
		$_where[] = array('up_id','=',$upid);
		$lists = Db::name('content_cat')->where($_where)
					->field('id,cat_name,cate_name,type,up_id,status,sort,table_id,route')
					->order('sort asc,id asc')->select();

		$n = count($lists);
		$cat_all = array();
		if($n){
			$m=1;
			for($i=0;$i<$n;$i++){
				if($n==$m){//只有一个子栏目/最后一个栏目
					$pre = $icon[2];
				}else{
					$pre = $icon[1];
				}
					$pad = $space ? $icon[0] : "";
				$lists[$i]['name']=($space ? $space.$pre : "").$lists[$i]['cat_name'];
				$cat_all[]=$lists[$i];
				$cat_bbb=$this->getSonLists($lists[$i]['id'], $space.$pad."&nbsp;"); //递归下一级分类
				if(count($cat_bbb)){//包含子栏目 合并
					$cat_all=array_merge($cat_all,$cat_bbb);
				}
				$m++;
			}
		}
		return $cat_all;
	}
/**
 * 修改分类的路径
**/
	public function edit_cat_path($cat_id,$up_id=0){
		$_old_data = Db::name('content_cat_path')->where('id','in',$cat_id.','.$up_id)->field('id,path')->select();
		ArrayKey($_old_data);
		if(!isset($_old_data[$cat_id])){
			$_add_data = array();
			$_add_data['id'] = $cat_id;
			if(!isset($_old_data[$up_id])){
				$_add_data['path'] = '0-'.$cat_id.'-';
			}else{
				$_add_data['path'] = $_old_data[$up_id]['path'].$cat_id.'-';
			}
			Db::name('content_cat_path')->insert($_add_data);

			$_add_page = array();
			$_add_page['id'] = $cat_id;
			$_add_page['update_time'] = time();
			Db::name('content_page')->insert($_add_page);

		}else{
			$_old_path = $_old_data[$cat_id]['path'];
			if(!isset($_old_data[$up_id])){
				$_new_path = '0-'.$cat_id.'-';
			}else{
				$_new_path = $_old_data[$up_id]['path'].$cat_id.'-';
			}
			if($_old_path != $_new_path){
				$_sql = "UPDATE `".config('database.prefix')."content_cat_path` SET `path`= replace(path,'".$_old_path."','".$_new_path."') WHERE (path like '".$_old_path."%' )";
				Db::query($_sql);
			}
		}
		cache('cat_all_data_url',null);
	}

	public function get_table_info($table_id = 0){
		if($table_id == 0){
			return false;
		}
		$_table_info = Db::name('content_model_table')->where('is_del','=','0')->where('id','=',$table_id)->find();
		// return $_table_info;
		if(!$_table_info){
			return false;
		}
		$_field_lists = Db::name('content_model_field')
				->where('table_id','=',$table_id)
				->where('status','=','1')
				->field('field_name,cname,type,attr_type,is_nav,is_search,is_required,type_group_id')
				->order('type_group_id asc,sort asc,id asc')->select();

		// return $_field_lists;

		if(!$_field_lists){
			return false;
		}

		$_data = array();
		$_data['table_name'] = 'content_lists_'.$_table_info['id'];

		$_type_group = nl2array($_table_info['type_group']);
		$_group_id = array();
		$type_group = array();
		if($_type_group){
			foreach($_type_group as $k=>$v){
				$type_group[] = array(
						'id' => $k,
						'name' => $v,
					);
				$_group_id[] = $k;
			}
		}else{
			$type_group[0] = array(
					'id' => '0',
					'name' => '基础信息',
				);
			$_group_id[] = 0;
		}

		$type_group[99] = array(
				'id' => '99',
				'name' => 'SEO信息',
			);
		$_group_id[] = 99;
		$_data['type_group'] = $type_group;

		$_data['nav'] = array();
		$_data['search'] = array();
		$_data['lists'] = array();

		foreach($_field_lists as $k=>$v){
			if($v['is_nav'] == 1){
				$_data['nav'][] = $v;
			}
			if($v['is_search'] == 1){
				$_data['search'][] = $v;
			}
			$_data['lists'][] = $v;
			$type_group_id = 0;
			if(in_array($v['type_group_id'],$_group_id)){
				$type_group_id = $v['type_group_id'];
			}

			$_data['lists_group'][$type_group_id][] = $v;
		}

		return $_data;
	}

	public function get_cat_lists(){
		$c_name = 'wy_content_get_cat_lists';
		$lists = cache($c_name);
		if(!$lists){
			$_where = array();
			$_where[] = array('is_del','=','0');
			$_where[] = array('status','=','1');
			$lists = Db::name('content_cat')->where($_where)
				->field('id,cat_name,cate_name,type,up_id,table_id')
				->order('sort asc,id asc')->select();
			cache($c_name,$lists,3600);
		}
		return $this->bl_cat_lists(0,$lists);
	}

	public function get_cat_name($id){
		$c_name = 'wy_content_get_cat_lists';
		$lists = cache($c_name);
		if(!$lists){
			$_where = array();
			$_where[] = array('is_del','=','0');
			$_where[] = array('status','=','1');
			$lists = Db::name('content_cat')->where($_where)
				->field('id,cat_name,cate_name,type,up_id,table_id')
				->order('sort asc,id asc')->select();
			cache($c_name,$lists,3600);
		}
		$_cat_name = '';
		if($lists){
			foreach($lists as $k=>$v){
				if($v['id'] == $id){
					$_cat_name = $v['cat_name'];
					break;
				}
			}
		}
		return $_cat_name;
	}

	private function bl_cat_lists($up_id,$lists){
		$_data = array();
		if($lists){
			foreach($lists as $k=>$v){
				if($v['up_id'] == $up_id){
					unset($lists[$k]);

					$_v_data = array();
					$_v_data['id'] = $v['id'];
					// $_v_data['icon'] = $v['id'];
					$_v_data['name'] = $v['cat_name'];
					// $_v_data['ename'] = $v['cate_name'];
					$_v_data['type'] = $v['type'];

					$_v_data['wy_icon'] = $this->wy_icon($v['type']);
					// $_v_data['table_id'] = $v['table_id'];
					$_v_data['wy_href'] = $this->get_content_url($v['id'],$v['table_id'],$v['type']);//url网址
					$_v_data['spread'] = true;//true 展开 false 关闭

					$_v_data['children'] = $this->bl_cat_lists($v['id'],$lists);

					$_data[] = $_v_data;
				}
			}
		}
		return $_data;
	}
	private function wy_icon($type=0){
		$_icon[1] = '&#xe60a;'; // 列表
		$_icon[2] = '&#xe621;'; // 单页
		$_icon[3] = '&#xe630;'; // 栏目
		$_icon[4] = '&#xe609;'; // 跳转
		$_icon[0] = '&#xe674;'; // 外链
		return $_icon[$type];
	}
	private function get_content_url($cid,$table_id=0,$type=0){
		// 1.文章列表：文章的列表页面，该栏目允许发布文章
		// 2.单页详情：单页的详情页，只有单独一篇内容维护
		// 3.栏目页：有页面，为该下级子栏目的一个集合页面
		// 4.跳转页：无页面，不能管理，访问自动调整到当前子栏目的第一个子栏目
		// 0.外部链接：跳出链接地址，可以填写站内或者站外的网址

		switch($type){
			case 1:
				// return url('Content/lists',array('table_id'=>$table_id,'cid'=>$id));
				return url('Content/lists').'?table_id='.$table_id.'&cid='.$cid;
				break;
			case 2:
				// return url('Content/pages',array('cid'=>$id));
				return url('Content/pages').'?cid='.$cid;
				break;
			case 3:
				return '';
				break;
			case 4:
				return '';
				break;
			case 0:
				// return url('Content/links',array('cid'=>$id));
				return url('Content/links').'?cid='.$cid;
				break;
			default:
				return '';
		}
	}

}
